package com.example.canvastest;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;

/**
 * Created by 700 on 2018/7/30.
 */

public class DrawRectView extends View{

    //1/4 定义变量
    private Paint mRectPaintStroke;
    private Paint mRectPaintFill;
    private int[] rect1 = new int[]{10, 10 , 110, 110};
    private int[] rect2 = new int[]{10, 310 , 110, 410};

    //2/4 三个构造函数
    public DrawRectView(Context context, AttributeSet attrs,
                              int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public DrawRectView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public DrawRectView(Context context) {
        super(context);
    }

    //3/4  初始化画笔
    private void initView(){
        mRectPaintStroke = new Paint();
        mRectPaintFill = new Paint();
        mRectPaintFill.setColor(getResources().getColor(
                android.R.color.holo_blue_bright));
        mRectPaintFill.setStyle(Paint.Style.FILL);

        mRectPaintStroke.setColor(getResources().getColor(
                android.R.color.holo_blue_bright));
        mRectPaintStroke.setStyle(Paint.Style.STROKE);
        mRectPaintStroke.setStrokeWidth(10);
    }

    //4/4  开画
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        initView();
        canvas.drawRect(rect1[0],rect1[1],rect1[2],rect1[3],mRectPaintStroke);

        canvas.drawRect(rect2[0],rect2[1],rect2[2],rect2[3],mRectPaintFill);
    }
}
